﻿'Ajouter un évènement permettant de savoir si la listView est scrollée

Public Class PTIC_ListView

    Private wParam As Integer
    Private Const WM_HSCROLL As Integer = &H114
    Private Const WM_MOUSEWHEEL As Integer = &H20A
    Private Const WM_VSCROLL As Integer = &H115
    Private Const SB_HORIZONT As Integer = &H0
    Private Const SB_VERTICAL = &H1

    Public Declare Function GetScrollPos Lib "user32" (ByVal hWnd As System.IntPtr, ByVal nBar As Integer) As Integer

    Public Event HScrollChanged As ScrollEventHandler
    Public Event VScrollChanged As ScrollEventHandler

    Public Shared Function HIWORD(ByVal n As Integer) As Integer
        Return ((n >> &H10) And &HFFFF)
    End Function
    Public Shared Function LOWORD(ByVal n As Integer) As Integer
        Return (n And &HFFFF)
    End Function

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
        MyBase.WndProc(m)

        Select Case m.Msg

            Case WM_HSCROLL
                wParam = m.WParam.ToInt32
                RaiseEvent HScrollChanged(Me, New ScrollEventArgs(EventType(LOWORD(wParam)), GetScrollPos(Me.Handle, SB_HORIZONT)))
                Exit Select

            Case WM_VSCROLL
                wParam = m.WParam.ToInt32
                RaiseEvent VScrollChanged(Me, New ScrollEventArgs(EventType(LOWORD(wParam)), GetScrollPos(Me.Handle, SB_VERTICAL)))
                Exit Select

            Case WM_MOUSEWHEEL
                wParam = m.WParam.ToInt32
                RaiseEvent VScrollChanged(Me, New ScrollEventArgs(EventType(LOWORD(wParam)), GetScrollPos(Me.Handle, SB_VERTICAL)))
                Exit Select

            Case Else

        End Select

    End Sub

    Private Function EventType(ByVal wParam As Integer) As ScrollEventType
        If wParam < [Enum].GetValues(GetType(ScrollEventType)).Length Then
            Return DirectCast(wParam, ScrollEventType)
        Else
            Return ScrollEventType.EndScroll
        End If
    End Function

#Region "Création d'une instance"
    Public Sub New()

        ' Cet appel est requis par le Concepteur Windows Form.
        InitializeComponent()

        'Activation du double buffering
        'SetStyle(ControlStyles.UserPaint, True)
        'SetStyle(ControlStyles.AllPaintingInWmPaint, True)
        SetStyle(ControlStyles.OptimizedDoubleBuffer, True)

    End Sub
#End Region

End Class
